package com.juzhencms.apps.busi.web;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.base.busi.service.RemoteClientService;
import com.juzhencms.apps.base.common.util.WebUtil;

import lombok.extern.slf4j.Slf4j;


@Controller
@Slf4j
@RequestMapping("/cacheManager")
public class CacheManagerController extends BaseController{
	
	@Autowired
	private RemoteClientService remoteClientService;

	
	@RequestMapping(value="/gridlistData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String gridlistData(HttpServletRequest request){
		List dataList=new ArrayList();
		
		//搜索参数
		if(request.getParameter("node_id")!=null && !request.getParameter("node_id").toString().trim().equals("")){
			//log.info(request.getParameter("node_id"));
			
			int nodeId=new Integer(request.getParameter("node_id"));
			
			Map result=remoteClientService.callByNodeId(nodeId,"cacheServiceApi", "getCacheList",null, null,Map.class);
			if(result!=null && result.get("result")!=null && result.get("result").toString().equals("1")) {
				dataList=(List)result.get("data");
			}
		}
		
		/*List dataList=new ArrayList();
		Object[] names=cacheManager.getCacheNames().toArray();
		for(int i=0;i<names.length;i++){
			String name=(String)names[i];
			net.sf.ehcache.Cache cache=(net.sf.ehcache.Cache)cacheManager.getCache(name).getNativeCache();
			
			Map row=new HashMap();
			row.put("id", name);
			row.put("num", cache.getSize());
			
			dataList.add(row);
		}
		
		//拼装返回的数据
		data.put("data", dataList.toArray());*/

		
		return WebUtil.resultString(dataList.toArray(),request);
	}
	
	@RequestMapping(value="/cleanData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String cleanData(HttpServletRequest request){

		//System.out.println(request.getParameter("data"));
		Map data = JSON.parseObject(request.getParameter("data"), Map.class); 
		
		int nodeId=new Integer(data.get("node_id").toString());
		
		Map param=new HashMap();
		param.put("names", data.get("id"));
		Map resultData=remoteClientService.callByNodeId(nodeId,"cacheServiceApi", "cleanCache",new Class[] {Map.class}, new Object[] {param},Map.class);
		
		Map result=new HashMap();
		if(resultData!=null && resultData.get("result")!=null && resultData.get("result").toString().equals("1")) {
			result.put("result",1);
			result.put("msg", "操作成功");
		}else {
			result.put("result",0);
			result.put("msg", "操作失败");
		}
		
		/*String id=null;
		if(data.get("id")!=null){
			id=data.get("id").toString();
		}
		
		
		if(id==null || id.equals("")){
		}else{	
			
			Object[] names=cacheManager.getCacheNames().toArray();
			
			List nameList=Arrays.asList(names);
			
	        String[] idArray = id.split(",");
	        for(int i=0;i<idArray.length;i++){
	        	String cacheName=idArray[i];
	        	if(cacheName!=null && !cacheName.equals("")){
	        		//清除缓存
	        		if(!nameList.contains(cacheName))
	        			continue;
	        		
	        		net.sf.ehcache.Cache cache=(net.sf.ehcache.Cache)cacheManager.getCache(cacheName).getNativeCache();
	        		if(cache!=null){
	        			cache.removeAll();
	        		}
	        	}
	        }
	        
		}*/
		
		return WebUtil.resultString(result,request);
	}
	
	
	
	
}
